'''
 * CaRComp Execution Environment
 * 
 * Copyright (C) 2011 Taylor L. Riche
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
'''

from carcomp.framework.file_io import file_reader, file_writer
from carcomp.framework import connector, application

class Wrapper(object):
    '''
    classdocs
    '''

    def __init__(self):
        '''
        Constructor
        '''
        self._comp_list = []
        self._conn_list = []
        self._reader_list = []
        self._writer_list = []
        self._app = application.Application()

    def add_abstraction(self, component, input_pairs, output_pairs):
        '''Adds an abstraction or component to the wrapper and creates file readers
           and writers for the input and output ports. Exceptions are thrown if no
           files are given for a given port (or if that file does not exist).
        '''
        self._comp_list.append(component)
        for input_tuple in input_pairs:
            fr = file_reader.FileReaderComponent(input_tuple[1])
            self._comp_list.append(fr)
            self._reader_list.append(fr)
            conn = connector.Connector(fr.get_output_endpoint(), component.get_endpoint(input_tuple[0]))
            self._conn_list.append(conn)
        for output_tuple in output_pairs:
            fw = file_writer.FileWriterComponent(output_tuple[1] + '_TEST.txt')
            self._comp_list.append(fw)
            self._writer_list.append(fw)
            conn = connector.Connector(component.get_endpoint(output_tuple[0]), fw.get_input_endpoint())
            self._conn_list.append(conn)
        self._app.add_components(self._comp_list)
        self._app.add_connections(self._conn_list)
        
    def run(self):
        self._app.resolve_abstractions()
        self._app.create_threads()
        self._app.execute(True, .1)